﻿@inject IDataSourceService DataSourceService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService

<MudCard>
    <MudCardContent>
        <SearchFormPartial Model="_searchRequestDto" QueryHandler="SearchButtonClick"></SearchFormPartial>
        <SearchTablePartial @ref="_requestTable" T="DataSourceInfoResponseDto" ServerData="ServerReload"
                            SearchPartial="_searchPartial"
                            AddHandler="AddHandler"
                            EditHandler="EditHandler">

        </SearchTablePartial>
    </MudCardContent>
</MudCard>

@code {

    private DataSourceSearchRequestDto _searchRequestDto = new();
    private IEnumerable<DataSourceInfoResponseDto> _pagedData = new List<DataSourceInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<DataSourceInfoResponseDto> _requestTable = new();
    private int _totalItems;

    private DialogOptions _dialogOptions = new()
        {
            CloseButton = true,
            FullWidth = true,
            DisableBackdropClick = true
        };

    [CascadingParameter]
    public DataSourceIndex? DataSourceIndex { get; set; }

    async Task SearchButtonClick()
    {
        await _requestTable.ReloadServerData();
    }

    void AddHandler()
    {
        DataSourceIndex?.SetDynamicComponentType(typeof(DataSourceEdit));
    }

    void EditHandler(DataSourceInfoResponseDto model)
    {
    }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    private async Task<TableData<DataSourceInfoResponseDto>> ServerReload(TableState state)
    {
        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var result = await DataSourceService.QueryDataSourcesByPage(_searchRequestDto);

        if (result is { Succeeded: true })
        {
            _pagedData = result.Data;
            _totalItems = result.TotalCount;
        }

        return new TableData<DataSourceInfoResponseDto>() { TotalItems = _totalItems, Items = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        _searchPartial.IsAdd = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.DataSource.CreateDataSource))).Succeeded;
        _searchPartial.IsEdit = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.DataSource.UpdateDataSource))).Succeeded;
        _searchPartial.IsDelete = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.DataSource.DeleteDataSource))).Succeeded;
        //_searchPartial.IsImportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Database.ImportDatabase))).Succeeded;
        //_searchPartial.IsExportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Database.ExportDatabase))).Succeeded;
    }


}